Skip to main content

Overview

In our application, we support two types of event handling mechanisms:

  1. Application events (from our NestJS API)
  2. Authentication events (from Keycloak)

These mechanisms allow us to capture and process events for various purposes, such as logging and authentication-related activities.

Introduction to Event Handling

Application Event Handling

Event handling within our application involves emitting and listening to events. This mechanism facilitates communication between different parts of the application in a decoupled manner, enabling features like logging, analytics, and other cross-cutting concerns without tightly coupling components.

Emitting Events

Our application includes an event emitter service that various components can use to emit necessary events. This service provides a consistent and effective method for managing event emissions throughout the application, enhancing extensibility by allowing the service to be injected into required components.

Listening to Events

Listener services in our application have event-specific listeners that manage event handling uniformly and effectively. These listeners can forward events for further processing, such as to a structured logger or for making a database call.

  1. Our application supports asynchronous listeners, which are beneficial for latency-sensitive services like moderation endpoints. Asynchronous configuration ensures that event handling does not consume latency bandwidth, maintaining optimal performance.
  2. Both synchronous and asynchronous listeners can be attached to the same emitted events, allowing for synchronous logging while performing asynchronous database calls.

Authentication Event Handling

Keycloak provides out-of-the-box event handling for various kinds of authentication, admin, and client events. This is particularly useful for capturing and processing events related to user authentication and authorization activities.

Use Cases

  • Event handling is applicable to various use cases within our application. Below are some primary use cases:

1. Logging

Logging is a primary use case for event handling in our application. By emitting events at various points, we can capture detailed logs that provide insights into the application's behavior and performance. Listener services capture these events and forward them to a structured logger, ensuring a consistent and comprehensive logging strategy across the application.

Leveraging the Logging Use-Case